package com.lrs.s2sh.xsd.wrapper;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lrs.s2sh.xsd.schemaSource.ActionDefinitionType;
import com.lrs.s2sh.xsd.schemaSource.ActionDefinitionsType;
import com.lrs.s2sh.xsd.schemaSource.FieldDefinitionType;
import com.lrs.s2sh.xsd.schemaSource.FieldDefinitonsType;
import com.lrs.s2sh.xsd.schemaSource.PageDataType;
import com.lrs.s2sh.xsd.schemaSource.PageType;
import com.lrs.s2sh.xsd.schemaSource.PagesType;
import com.lrs.s2sh.xsd.util.FlexibleConstants;

/**
 * <p>Title: 		EESysCfgWrapper</p>
 * <p>Description: 	The wrapper for EE page Customized Configuration, which implements some additional common used methods.</p>
 * <p>Copyright: 	Copyright (c) 2007</p>
 * <p>Company: 		Lombard Risk Systems Ltd</p>
 * 
 * @author Michael Zhang
 * @created 10 Nov 2010
 * @version $Revision: 1.00 $
 */
public class EEPageDataCfgWrapper implements Serializable {

    private final static long serialVersionUID = 1L;
	private PageDataType pageDataCfg;
	
	private Map<String,Map> sysDataCfg;
	
	public EEPageDataCfgWrapper(PageDataType pageDataCfg) {
		super();
		this.pageDataCfg = pageDataCfg;
		init();
	}

	public void setPageDataCfg(PageDataType pageDataCfg) {
		this.pageDataCfg = pageDataCfg;
		init();
	}
	
	public PageDataType getPageDataCfg() {
		return pageDataCfg;
	}
	
	public Map<String, FieldDefinitionType> getEEFieldByPageId(String pageId) {
		return (Map<String, FieldDefinitionType>)sysDataCfg.get(pageId).get(FlexibleConstants.FLEXIBLE_EE_DATA_FIELDS);
		
	}
	
	public Map<String, ActionDefinitionType> getEEActionByPageId(String pageId) {
		return (Map<String, ActionDefinitionType>)sysDataCfg.get(pageId).get(FlexibleConstants.FLEXIBLE_EE_DATA_ACTIONS);
		
	}
	
	public List<ActionDefinitionType> getActionDefs(String pageId) {
		Map<String, ActionDefinitionType> actionDefMap = getEEActionByPageId(pageId);
		
		return (List<ActionDefinitionType>)actionDefMap.values();
	}
	
	
	public List<FieldDefinitionType> getFieldsByPageId(String pageId) {
//		PageType pageType = sysDataCfg.get(pageId);
//		return pageType.getFields().getField();
		
		return null;
	}

	private void init() {
		PagesType page = pageDataCfg.getPages();
		if(page == null) {
			page = new PagesType();
		}
		
		List<PageType> pages = page.getPage();
		sysDataCfg = new HashMap<String,Map>();
		for (Iterator iter = pages.iterator(); iter.hasNext();) {
			Map<String, Map> pageCfgMap = new HashMap<String, Map>();
			PageType pageType = (PageType) iter.next();
			
			//init page fields
			FieldDefinitonsType fieldDefType = pageType.getFields();
			if(fieldDefType == null) {
				fieldDefType = new FieldDefinitonsType(); 
			}
			
			List<FieldDefinitionType> fields = fieldDefType.getField();
			Map<String, FieldDefinitionType> fieldMap = new HashMap<String, FieldDefinitionType>();
			for (Iterator<FieldDefinitionType> iterator = fields.iterator(); iterator.hasNext();) {
				FieldDefinitionType field = iterator.next();
				
				fieldMap.put(field.getId(), field);
			}
			pageCfgMap.put(FlexibleConstants.FLEXIBLE_EE_DATA_FIELDS, fieldMap);
			
			//init page actions
			ActionDefinitionsType actionDefsType = pageType.getActions();
			if(actionDefsType == null) {
				actionDefsType = new ActionDefinitionsType(); 
			}
			List<ActionDefinitionType> actions = actionDefsType.getAction();
			Map<String, ActionDefinitionType> actionMap = new HashMap<String, ActionDefinitionType>();
			for (Iterator<ActionDefinitionType> iterator = actions.iterator(); iterator.hasNext();) {
				ActionDefinitionType actionDefType = iterator.next();
				actionMap.put(actionDefType.getId(), actionDefType);
				
			}
			pageCfgMap.put(FlexibleConstants.FLEXIBLE_EE_DATA_ACTIONS, actionMap);
			
			sysDataCfg.put(pageType.getId(), pageCfgMap);
		}
	}
	
}
